package com.flute.framework.search.result.xmlresult.multi;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class XpathExDistributer {
	private Set<IXpathExParseEnable> parserSet;

	public XpathExDistributer() {
		parserSet = new HashSet<IXpathExParseEnable>();
	}

	public XpathExDistributer(Set<IXpathExParseEnable> parserSet) {
		this.parserSet = parserSet;
	}

	public boolean add(IXpathExParseEnable parser) {
		return parserSet.add(parser);
	}

	public boolean add(Collection<IXpathExParseEnable> col) {
		return parserSet.addAll(col);
	}

	public boolean remove(IXpathExParseEnable parser) {
		return parserSet.remove(parser);
	}

	public void clear() {
		parserSet.clear();
	}

	public IXpathExParseEnable distribute(String xpath)
			throws XpathExParserNotFoundException {
		for (Iterator<IXpathExParseEnable> iter = parserSet.iterator(); iter
				.hasNext();) {
			IXpathExParseEnable curr = iter.next();
			if (curr.correspond(xpath)) {
				return curr;
			}
		}
		throw new XpathExParserNotFoundException("xpath=" + xpath);
	}

	public static XpathExDistributer getAllRegisterDistributer() {
		Set<IXpathExParseEnable> parserSet = new HashSet<IXpathExParseEnable>();
		parserSet.add(new NoExXpathParser());
		parserSet.add(new SimpleChildXpathParser());
		return new XpathExDistributer(parserSet);
	}

	public static void main(String[] args)
			throws XpathExParserNotFoundException {
		XpathExDistributer distributer = getAllRegisterDistributer();
		System.out.println(distributer.distribute("//songs/song"));
		System.out.println(distributer
				.distribute("//songs//#[child=b parent=d e=f]"));
	}
}
